package com.coolwen.service.student;

import com.alibaba.fastjson.JSON;
import com.coolwen.dao.StudentMapper;
import com.coolwen.dao.TotalMapper;
import com.coolwen.entity.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by nieyu on 2016/8/5.
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService{

    private static final Logger LOGGER = Logger.getLogger(StudentServiceImpl.class);

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void insert(Student student) {

    }

    @Override
    public int insertSelective(Student student) {

        return studentMapper.insertSelective(student);
    }

    @Override
    public List<Student> selectAllInfo() {
        return studentMapper.selectAllInfo();
    }

    @Override
    public Pager<Student> find(String grade,String department,String major) {
        int pageSize = SystemContext.getPageSize();
        int pageOffset = SystemContext.getPageOffset();

        Pager<Student> pages = new Pager<Student>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", "%" + grade + "%");

        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        params.put("pageSize",  pageSize);
        params.put("pageOffset", pageOffset);


        List<Student> datas = studentMapper.find(params);

        int totalRecord = studentMapper.find_count(params);
        pages.setDatas(datas);
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);
        return pages;
    }

    @Override
    public List<Student> findAll(String grade, String department, String major) {
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", "%" + grade + "%");

        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        List<Student> datas = studentMapper.find(params);
        return datas;
    }

    @Override
    public Pager<Student> selectStudentCourseInfo(String grade, String department, String major, String course) {
        int pageSize = SystemContext.getPageSize();
        List<Integer> grades = new ArrayList<Integer>();
        int pageOffset = SystemContext.getPageOffset();

        Pager<Student> pages = new Pager<Student>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {

            int g1 = Integer.valueOf(grade);
            int g2 = g1 + 1;
            int g3 = g1 + 2;
            int g4 = g1 + 3;
            grades.add(g1);
            grades.add(g2);
            grades.add(g3);
            grades.add(g4);
            params.put("grade", grades);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        if (course != null && !course.equals("")) {
            params.put("course", "%" + course + "%");
        }
        params.put("pageSize", pageSize);
        params.put("pageOffset", pageOffset);


        List<Student> datas = studentMapper.selectStudentCourseInfo(params);

        int totalRecord = studentMapper.selectCourse_count(params);
        pages.setDatas(datas);
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);
        return pages;
    }

    @Override
    public Pager<Student> selectStudentOneCourseInfo(String grade, String department, String major, String course) {

        int pageSize = SystemContext.getPageSize();
        int pageOffset = SystemContext.getPageOffset();

        Pager<Student> pages = new Pager<Student>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", "%" + grade + "%");

        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        if (course != null && !course.equals("")) {
            params.put("course", "%" + course + "%");
        }
        params.put("pageSize",  pageSize);
        params.put("pageOffset", pageOffset);

        List<Student> datas = studentMapper.selectStudentOneCourseInfo(params);

        int totalRecord = studentMapper.selectOneCourse_count(params);
        pages.setDatas(datas);
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);

        return pages;
    }

    @Override
    public Pager<Collect> collect(String grade, String department, String major) {

        int pageSize = SystemContext.getPageSize();
//        int pageSize = 100;
        int pageOffset = SystemContext.getPageOffset();

        Pager<Collect> pages = new Pager<Collect>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", grade);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        params.put("pageSize", pageSize);
        params.put("pageOffset", pageOffset);

        List<Collect> collects = studentMapper.collect(params);

        int totalRecord = studentMapper.collect_count(params);
        LOGGER.info("查找结果11111111" + JSON.toJSON(pages.getDatas()));
        pages.setDatas(collects);
        LOGGER.info("查找结果" + JSON.toJSON(pages.getDatas()));
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);
        return pages;
    }

    @Override
    public List<Collect> collectAll(String grade, String department, String major) {

        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", grade);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }

        List<Collect> collects = studentMapper.collect(params);

        return collects;
    }

    @Override
    public Pager<Collect> collectBySemester(String grade, String department, String major, String semester) {
        int pageSize = SystemContext.getPageSize();
//        int pageSize = 100;
        int pageOffset = SystemContext.getPageOffset();

        Pager<Collect> pages = new Pager<Collect>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", grade);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        if (semester != null && !semester.equals("")) {
            params.put("semester", "%" + semester + "%");
        }
        params.put("pageSize", pageSize);
        params.put("pageOffset", pageOffset);

        List<Collect> collects = studentMapper.collectBySemester(params);

        int totalRecord = studentMapper.semester_count(params);
        LOGGER.info("查找结果11111111" + JSON.toJSON(pages.getDatas()));
        pages.setDatas(collects);
        LOGGER.info("查找结果" + JSON.toJSON(pages.getDatas()));
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);
        return pages;
    }

    @Override
    public List<Collect> collectBySemesterAll(String grade, String department, String major, String semester) {
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", grade);
        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        if (semester != null && !semester.equals("")) {
            params.put("semester", "%" + semester + "%");
        }

        List<Collect> collects = studentMapper.collectBySemester(params);

        return collects;
    }


}
